package com.he.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.he.dao.OrderDao;
import com.he.entity.*;
import com.he.feign.HotelFeign;
import com.he.login.UserUtil;
import com.he.service.IOrderPriceService;
import com.he.service.IOrderService;
import com.he.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, Orders> implements IOrderService {
    @Autowired
    private HotelFeign hotelFeign;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private IOrderPriceService priceService;

    /**
     * 计算订单价格
     * @param params
     * @return
     */
    @Override
    public OrderPriceResult getOrderPrice(OrderPriceParams params) {

        //获取房间信息
        ResultData<Room> rooms = hotelFeign.getRoomById(params.getRid());
        //总价
        BigDecimal allPrice = BigDecimal.valueOf(0);
        //价格明细列表
        List<List<String>> priceDetail = new ArrayList<>();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        for (RoomPrice roomPrice : rooms.getData().getPrices()){
            if(roomPrice.getDate().getTime() >= params.getBeginTime().getTime() &&
                    roomPrice.getDate().getTime() < params.getEndTime().getTime() ){
                //房间单价
                BigDecimal price = roomPrice.getPrice();
                //房价累加
                allPrice = allPrice.add(price);

                //设置明细
                List<String> list = new ArrayList<>();
                list.add(simpleDateFormat.format(roomPrice.getDate()));
                list.add(params.getRnumber() + "x" + price.doubleValue());
                priceDetail.add(list);
            }
        }

        //乘以房价数量
        allPrice = allPrice.multiply(BigDecimal.valueOf(params.getRnumber()));

        OrderPriceResult priceResult = new OrderPriceResult()
                .setAllprice(allPrice.doubleValue())
                .setPriceDetails(priceDetail);

        return priceResult;
    }

    /**
     * 下单
     * @param orders
     * @param params
     * @return
     */
    @Override
    public String insertOrder(Orders orders, OrderPriceParams params) {

        //获取当前登录用户
        User user = UserUtil.getUser();

        //计算总价格
        OrderPriceResult orderPrice = getOrderPrice(params);

        orders
            .setOid(UUID.randomUUID().toString().replace("-",""))
            .setAllPrice(orderPrice.getAllprice())
            .setNumber(params.getRnumber())
            .setDays(DateUtil.days(orders.getBeginTime(),orders.getEndTime()))
            .setUid(user.getId());

        //保存订单对象
        orderDao.insert(orders);

        //保存价格详情信息
        List<OrderPriceDetails> orderPriceDetails = new ArrayList<>();
        ResultData<Room> room = hotelFeign.getRoomById(orders.getRid());
        List<RoomPrice> prices = room.getData().getPrices();
        for (RoomPrice price : prices) {
            if (price.getDate().getTime() >= params.getBeginTime().getTime() &&
                    price.getDate().getTime() < params.getEndTime().getTime()) {
                OrderPriceDetails orderPriceDetail = new OrderPriceDetails()
                        .setOid(orders.getOid())
                        .setPrice(price.getPrice().doubleValue())
                        .setTime(price.getDate());

                //保存到订单的价格详情列表
                orderPriceDetails.add(orderPriceDetail);
            }
        }

        //保存详情
        priceService.saveBatch(orderPriceDetails);

        return orders.getOid();
    }
}
